package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.utils.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    private EmployeeMapper employeeMapper ;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void insert(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        if (ids!=null && ids.length>0){
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }

    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Override
    public void update(Employee employee,Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRelation(employee.getId());
        if (ids!=null && ids.length>0){
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }

    @Override
    public Employee selectById(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee ;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees ;
    }

    @Override
    public PageInfo<Employee> query(EmployeeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.queryAll(qo);
        return new PageInfo<>(employees);
    }

    @Override
    public boolean checkedUsername(String username) {
        Employee employee = employeeMapper.checkedUsername(username);
        return employee != null ;
    }

    @Override
    public Employee login(String username, String password) {
        Employee employee =employeeMapper.selectByUsernameAndPassword(username,password);
        return employee;
    }

    @Override
    public Workbook exportXls() {
        //创建Excel
        Workbook wb = new HSSFWorkbook();
        //创建sheet表单
        Sheet sheet = wb.createSheet("员工表");
        //创建行
        Row headRow = sheet.createRow(0);
        headRow.createCell(0).setCellValue("用户名");
        headRow.createCell(1).setCellValue("姓名");
        headRow.createCell(2).setCellValue("年龄");

        //获取数据库中数据
        List<Employee> employees = employeeMapper.selectAll();
        //创建表格
        for (int i = 0; i < employees.size(); i++) {
            Row row = sheet.createRow(i+1);
            Employee employee = employees.get(i);
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getAge());
        }
        return wb ;
    }

    @Override
    public void importXls(Workbook wb) {
        //获取表单
        Sheet sheet = wb.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1 ; i<lastRowNum ; i++){
            Row row = sheet.getRow(i);
            String username = row.getCell(0).getStringCellValue();
            String name = row.getCell(1).getStringCellValue();
            int age = (int)row.getCell(2).getNumericCellValue();
            Employee employee= new Employee();
            employee.setUsername(username);
            employee.setName(name);
            employee.setAge(age);
            employee.setPassword("1");

            employeeMapper.insert(employee);
        }
    }

    @Override
    public boolean isAdmin() {
        return UserContext.getEmployee().isAdmin();
    }


}
